package tcpServerAndClient;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

import cmdParser.CmdParser;

public class TcpClient {
	private Socket socket;
	private OutputStream out;
	private InputStream in;
	
	public TcpClient(String ip) throws IOException{
		this.socket = new Socket(ip, 8001);;
	    out = socket.getOutputStream();
		in = socket.getInputStream();
	}
	
	public Socket getSocket() {
		return socket;
	}

	public void setSocket(Socket socket) {
		this.socket = socket;
	}

	public OutputStream getOut() {
		return out;
	}

	public void setOut(OutputStream out) {
		this.out = out;
	}

	public InputStream getIn() {
		return in;
	}

	public void setIn(InputStream in) {
		this.in = in;
	}

	public static void main(String[] args) throws UnknownHostException, IOException {
		TcpClient newTcpClient;
		InputStream in1;
		OutputStream out1;
		BufferedReader reader1;
		Scanner scan = new Scanner(System.in);
		boolean status = true;
		
		while(status){
			System.out.println("please enter a commad: ");
			String cmd = scan.nextLine();
			String[] params = CmdParser.cmdParseForClient(cmd);		//分析命令
			
			if(params != null){
				if(params[0].equals("LS")){
					//列出文件中文件的列表
					newTcpClient = new TcpClient("127.0.0.1");
					in1 = newTcpClient.getIn();
					out1 = newTcpClient.getOut();
					reader1 = new BufferedReader(new InputStreamReader(newTcpClient.getSocket().getInputStream()));
					
					out1.write("LS\n".getBytes());
					
					String lsList = reader1.readLine();
					System.out.println(lsList);
				}
				else if(params[0].equals("RM")){
					//删除操作
					newTcpClient = new TcpClient("127.0.0.1");
					in1 = newTcpClient.getIn();
					out1 = newTcpClient.getOut();
					reader1 = new BufferedReader(new InputStreamReader(newTcpClient.getSocket().getInputStream()));
					
					//发送: RM + 文件名字
					out1.write((params[0] + " " + params[1] + "\n").getBytes());
					
					//服务端返回删除完毕
					System.out.println(reader1.readLine());
				}
				else{
					//scp 操作
					newTcpClient = new TcpClient(params[2]);
					in1 = newTcpClient.getIn();
					out1 = newTcpClient.getOut();
					reader1 = new BufferedReader(new InputStreamReader(newTcpClient.getSocket().getInputStream()));
					
					//先发送提示： scp 文件名字,服务器获得之后就知道我们想传送文件过去
					out1.write(("SCP " + params[3] + "\n").getBytes());
					String t = reader1.readLine();
					//服务端会返回一个文件接收的确认信息 ack
					if(t.equals("ack")){
						File file = new File(params[1]);
						if(file.exists()){			//文件存在就发送过去
							InputStream in2 = new FileInputStream(file);
							byte[] buf = new byte[2];
							int len = 0;
							
							//读文件，输出去
							while((len = in2.read(buf)) != -1){					//这里可能存在阻塞
								out1.write(buf);
							}
							newTcpClient.getSocket().close();
							System.out.println("发送文件成功");
							
							in2.close();
							out1.close();
							in1.close();
							newTcpClient.getSocket().close();
						}
						else{
							System.out.println(params[1] + "文件不存在......");
							in1.close();
							out1.close();
							reader1.close();
							newTcpClient.getSocket().close();
						}
					}
				}
			}
		}
	}
}
